package com.yanqu.road.logic.activity.foxcard.game.sorcery;

import com.yanqu.road.entity.activity.foxcard.FoxBaseCard;
import com.yanqu.road.entity.activity.foxcard.FoxEffectCard;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardBuffConfig;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardGameLogic;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardShowInfo;
import com.yanqu.road.logic.activity.foxcard.game.eFoxCardShowType;
import com.yanqu.road.logic.activity.foxcard.game.eFoxCardTriggerType;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 随机给小丑牌加buff
 * 数量|buffid|buffid|buffid|0
 * 使用后，随机一张当前已拥有的小丑牌，增加多彩版本效果。
 * 1、已拥有其它版本的小丑牌不会算在内。 2、当前无可随机的小丑牌时，则无法使用。
 */
public class FoxCardSorceryUse2 extends FoxCardSorceryUseBase {
    @Override
    protected boolean sorceryUse(FoxCardGameLogic gameLogic) {
        int num = sorceryConfig.getParamList().get(0);

        List<FoxEffectCard> findCards = new ArrayList<>();
        gameLogic.getGameData().getEffectCards().stream().filter(card -> card.getVersionEffect() == 0).forEach(findCards::add);
        if (findCards.isEmpty()) {
            return false;
        }

        List<Integer> buffIds = new ArrayList<>();
        for (int i = 1; i < sorceryConfig.getParamList().size(); i++) {
            Integer buffId = sorceryConfig.getParamList().get(i);
            if (buffId > 0) {
                buffIds.add(buffId);
            }
        }
        if (buffIds.isEmpty()) {
            return false;
        }

        Integer buffId = RandomHelper.getRandomList(buffIds, 1).get(0);
        FoxCardBuffConfig buffConfig = gameLogic.getConfig().getBuffConfigMap().get(buffId);
        if (buffConfig == null) {
            return false;
        }

        Collections.shuffle(findCards);

        List<FoxBaseCard> triggerCards = new ArrayList<>();
        for (int i = 0; i < findCards.size() && i < num; i++) {
            FoxEffectCard effectCard = findCards.get(i);
            effectCard.setVersionEffect(buffId);
            gameLogic.dealEffectCardBuff(effectCard, eFoxCardTriggerType.ADD);
            triggerCards.add(effectCard);
        }

        FoxCardShowInfo showInfo = gameLogic.newFoxShow(eFoxCardShowType.VERSION_EFFECT.getValue());
        showInfo.setTriggerCards(triggerCards);
        showInfo.setShowValue(buffId);

        return true;
    }
}
